#!/bin/sh

### BEGIN INIT INFO
# Provides:     jameica
# Required-Start:   $local_fs $remote_fs $network
# Required-Stop:    $local_fs $remote_fs $network
# Default-Start:    3 5
# Default-Stop:     0 1 2 6
# Short-Description:    Jameica Application Server
# Description:      Starts the Jameica Application Server
### END INIT INFO

# function phpps "shameless stolen" from
# Copyright (c) 2000 Alexander V. Konstantinou <akonstan@acm.org>
#
# Permission to use, copy, modify, distribute and sell this software
# and its documentation for any purpose is hereby granted without fee,
# provided that the above copyright notice appear in all copies and
# that both that copyright notice and this permission notice appear
# in supporting documentation. Alexander V. Konstantinou makes no
# representations about the suitability of this software for any
# purpose.  It is provided "as is" without express or implied warranty.
#
# Description: prints out the php processes running on a Linux host.
#              Optional "-noflags" argument controls verbosity
#
# Requirements: Linux host with a /proc filesystem
#
# Updates: http://www.cs.columbia.edu/~akonstan/phpps
function phpps()
{
   NOFLAGS=0
   if [ -n "$1" -a "$1" = "-noflags" ]; then
      NOFLAGS=1
   fi

   echo "PID   Process"

   # For every file in the /proc file system
   FILES=`/bin/ls -1 /proc`

   for FILE in $FILES; do
      PROCESS_ID=$FILE
        STATUS_FILE=/proc/$FILE/status
        CMDLINE_FILE=/proc/$FILE/cmdline

        # Check if it is a process directory and that we have read access
        if [ -d "/proc/$FILE" -a -r $STATUS_FILE -a -r $CMDLINE_FILE -a "$FILE" != "/proc/self" ]; then
         PROCESS_NAME=`grep 'Name:' $STATUS_FILE | awk '{print $2}'`

          # Only interested in php processes
          if [ "$PROCESS_NAME" = "php" ]; then
               PARENT_PID=`grep 'PPid' $STATUS_FILE | awk '{print $2}'`

               # Figure out if process has a parent that is a php process
               ISROOT=0
               if [ $PARENT_PID -eq 1 ]; then
                 ISROOT=1
               else
                 PARENT_NAME=`grep 'Name:' /proc/$PARENT_PID/status | awk '{print $2}'`
                 if [ "$PARENT_NAME" != "php" ]; then
                      ISROOT=1
                 fi
               fi

               # If root php process print out
               if [ $ISROOT -ne 0 ]; then
                 PROCESS_CMDLINE="`cat $CMDLINE_FILE | tr '\000' ' '`"
               IGNORE_ARG=0
                 DESCR=
                 for ARG in $PROCESS_CMDLINE; do
                      if [ $NOFLAGS -eq 0 ]; then
                         DESCR="$DESCR $ARG"
                      else
                        if [ $IGNORE_ARG -eq 0 ]; then
                             if [ $ARG = "-classpath" -o $ARG = "-cp" ]; then
                               IGNORE_ARG=1
                             elif [ -z "`echo $ARG | grep '^-D'`" -a -z "`echo $ARG | grep '^-X'`" ]; then
                               DESCR="$DESCR $ARG"
                             fi
                           else
                               IGNORE_ARG=0
                        fi
                      fi
                 done
                 echo "$PROCESS_ID   $DESCR"
               fi
          fi
        fi
   done
}


#
# Determine the base and follow a runlevel link name.
#
base=${0##*/}
link=${base#*[SK][0-9][0-9]}

URDD_BIN=/var/www/html/urd/urdd.sh
PIDFILE=/var/run/urddsh.pid

### Please do not edit anything below this line! ########################
#                                                                       #

. /etc/rc.status

rc_reset

return=$rc_done
case "$1" in
   start)
      echo -n "Starting urdd "
      $URDD_BIN -D > /dev/null 2>&1 &
      sleep 1
      PID=`phpps -noflags | grep urdd | awk '{print $1}'`
      echo "urdd started with PID=$PID"
      rc_status -v
      echo $PID > $PIDFILE
      ;;
   stop)
      if test -e $PIDFILE ; then
         echo -n "Shutting down urdd "
         kill `cat $PIDFILE`
         rm -f $PIDFILE
         rc_status -v
      else
         echo "Not running"
      fi
      ;;
   reload|restart)
      $0 stop
      sleep 1
      $0 start
      ;;
   status)
      echo -n "Checking for urdd "
      if test -e $PIDFILE ; then
         echo " seems running"
      else
         echo " not running"
      fi
      ;;
   *)
      echo "Usage: $0 {start|stop|restart|status}"
      exit 1
      ;;
esac
rc_exit
